package c3au;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Date;

public class CentroMonitoreo {

	private ServerSocket server;
	private BufferedReader in;
	private Socket cliente;
	private InterfazCentroMonitoreo icm;
	
	public CentroMonitoreo(InterfazCentroMonitoreo interfaz){
		icm=interfaz;
		establecerConexion();
	}
	
	public void establecerConexion()
	{
		try {
			server= new ServerSocket(7854);
			cliente=server.accept();
			in = new BufferedReader(new InputStreamReader(cliente.getInputStream()));
			
			while(true)
			{
				String cadena=in.readLine();
				System.out.println(cadena);
				cadena=procesarTrama(cadena);
				icm.actualizar(cadena);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String procesarTrama(String trama){
		String[] minis=trama.split("@");
		Date fecha=new Date(Long.parseLong(minis[6]));
		System.out.println("Min:"+minis[6]);
		trama=trama.replace(minis[6], fecha+"");
		return trama;
	}
}
